package com.rongshu.sendmessage.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.wx.Constant_Third_ID;
import com.rongshu.sendmessage.util.wxPayUtil.PayListenerUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
//wxapi.WXPayEntryActivity
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constant_Third_ID.weixin_app_id);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.e("onPayFinish, errCode = " , resp.errCode+"");

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK://支付成功
//                    WxRsultEvent lWxRsultEvent = new WxRsultEvent();
//                    lWxRsultEvent.setWxPaySuccess(true);
//                    EventBus.getDefault().post(lWxRsultEvent);
                    PayListenerUtils.getInstance(this).addSuccess();
                    finish();
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED://没有授权
                    ToastUtils.show(WXPayEntryActivity.this, "未授权");
                    break;
                case BaseResp.ErrCode.ERR_UNSUPPORT://不支持
                    ToastUtils.show(WXPayEntryActivity.this, "不支持");
                    break;
                case BaseResp.ErrCode.ERR_COMM://可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
                    ToastUtils.show(WXPayEntryActivity.this, "微信参数错误，支付不成功");
                    PayListenerUtils.getInstance(this).addError();
                    finish();
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL://可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
                    PayListenerUtils.getInstance(this).addCancel();
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            ToastUtils.show(WXPayEntryActivity.this, "未知错误");
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                WXPayEntryActivity.this.finish();
            }
        }, 1000);
    }
}